package com.hdy.oa.web.action.department;

import com.hdy.DBUtils.JDBC;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.catalina.connector.Response;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/*
    * 在Servlet类上使用: @WebServlet，WebServlet注解中有哪些属性呢?
     name属性:用来指定Servlet的名字。等同于:<servlet-name>
     urlPatterns属性:用来指定Servlet的映射路径。可以指定多个字符串。<url-pattern>
     loadOnStartUp属性:用来指定在服务器启动阶段是否加载该Servlet。等同于: <load-on-startup>
     value属性:当注解的属性名是value的时候，使用注解的时候，value属性名是可以省略的。
     *
     注意:不是必须将所有属性都写上，只需要提供需要的。(需要什么用什么。)
     注意:属性是一个数组，如果数组中只有一个元素，使用该注解的时候，属性值的大括号可以省略。
     * 注解对象的使用格式:
     @注解名称(属性名=属性值,属性名=属性值,属性名=属性值....)

* */

@WebServlet("/dept/list")
public class DepListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String contextPath = request.getContextPath();
                //document.location.href="请求路径"
                //document.location="请求路径"
                //window.location.href="请求路径"
                //window.location="请求路径"
              out.print("       <!DOCTYPE html>");
              out.print("<html>");
              out.print("	<head>");
              out.print("		<meta charset='utf-8'>");
              out.print("		<title>部门</title>");
              //javascript
              out.print("<script type='text/javascript'>");
              out.print("          function del(No){");
              out.print("      var ok=window.confirm('您是否要删除？');");
              out.print("      if(ok){");
              out.print("          document.location.href='"+contextPath+"/dept/delete?deptNo='+No;");
              //out.print("          alert('删除成功！')");
              out.print("      }");
              out.print("  }");
              out.print("  </script>");

              out.print("	</head>");
              out.print("	<body>");
              out.print("		<div align='center'>");
              out.print("			<h1><font size='200pix' color='blue'>部门</font></h1>");
              out.print("		</div>");
              out.print("		");
              out.print("		<hr>");
              out.print("		<br><br><br>");
              out.print("		<table align='center' border='1pix' width='600pix' height='300pix'>");
              out.print("			<tr>");
              out.print("				<th>序号</th>");
              out.print("				<th>部门编号</th>");
              out.print("				<th>部门名称</th>");
              out.print("				<th>部门位置</th>");
              out.print("				<th>操作</th>");
              out.print("			</tr>");

        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;


        try {
            connection= JDBC.getConnection();
            String sql="select * from dept";
            ps=connection.prepareStatement(sql);
            rs= ps.executeQuery();
            int no=1;

            while(rs.next()){
                String deptNo=rs.getString(1);
                String name=rs.getString(2);
                String loc=rs.getString(3);
               out.print("			<tr>");
               out.print("				<td align='center'>"+(no++)+"</td>");
               out.print("				<td align='center'>"+deptNo+"</td>");
               out.print("				<td align='center'>"+name+"</td>");
               out.print("				<td align='center'>"+loc+"</td>");
               out.print("				<td align='center'><a href='"+contextPath+"/dept/details?deptNo="+deptNo+"'>详情</a> &nbsp;" +
                       "<a href='"+contextPath+"/dept/query?deptNo="+deptNo+"&no="+(no-1)+"'>修改</a> &nbsp;" +
                       "<a href='javascript:void(0)' onclick='del("+deptNo+")'>删除</a></td>");
               out.print("			</tr>");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBC.close(connection,ps,rs);
        }

      out.print("			");
      out.print("		</table>");
      out.print("		<br><br><br>");
      out.print("		<hr>");
      out.print("		<a href='"+contextPath+"/add.html'><font size='5cm' color='red'>新增部门</font></a>");
      out.print("	</body>");
      out.print("<html>");
    }
}
